//
//  ROAsset.m
//  Runone
//
//  Created by runone02 on 16/12/29.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "ROAsset.h"

#define screenScale [UIScreen mainScreen].scale
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height
@interface ROAsset ()
@property (nonatomic, strong) UIImage *orignImage;
@property (nonatomic, strong) UIImage *thumbImage;
@property (nonatomic, strong) UIImage *previewImage;
@property (nonatomic, assign) CGFloat imageSize;
@end

@implementation ROAsset

-(void)setSelected:(BOOL)selected {
    _selected = selected;
}

- (void)setAsset:(PHAsset *)asset {
    _asset = asset;
}

- (UIImage *)orignImage {
    if (_orignImage) {
        return _orignImage;
    }
    
    __block UIImage *resultImage;
    
    PHImageRequestOptions *phImageRequestOptions = [[PHImageRequestOptions alloc] init];
    phImageRequestOptions.synchronous = YES;
    [[PHImageManager defaultManager] requestImageForAsset:self.asset
                                               targetSize:PHImageManagerMaximumSize
                                              contentMode:PHImageContentModeDefault
                                                  options:phImageRequestOptions
                                            resultHandler:^(UIImage *result, NSDictionary *info) {
                                                resultImage = result;
                                            }];
    _orignImage = resultImage;
    return resultImage;
    
}

//如果需要获取任何图片相关的信息可以使用这个方法
//UIImage *displaySizeImage;
//NSURL *fullSizeImageURL;
//fullSizeImageOrientation; // EXIF value

- (CGFloat)imageSize
{
    if (_imageSize == 0) {
        [self getImageInfo];
    }
    return _imageSize;
}

- (void)getImageInfo
{
    PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];
    [self.asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
        
        long long fileSize = [ROAsset fileSizeAtPath:contentEditingInput.fullSizeImageURL.path];
        self.imageSize = fileSize/1000000.;
    }];
}

- (void)fetchOriginImageWithCompletion:(void (^)(UIImage *, NSDictionary *))completion withProgressHandler:(PHAssetImageProgressHandler)phProgressHandler
{
    if (_orignImage) {
        // 如果已经有缓存的图片则直接拿缓存的图片
        if (completion) {
            completion(_orignImage, nil);
        }
    } else {
        PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
        imageRequestOptions.networkAccessAllowed = YES; // 允许访问网络
        imageRequestOptions.progressHandler = phProgressHandler;
        [[PHImageManager defaultManager] requestImageForAsset:self.asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:imageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
            // 排除取消，错误，低清图三种情况，即已经获取到了高清图时，把这张高清图缓存到 _originImage 中
            BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue];
            if (downloadFinined) {
                _orignImage = result;
            }
            if (completion) {
                completion(result, info);
            }
        }];
    }
}

- (UIImage *)thumbnailWithSize:(CGSize)size {
    if (_thumbImage) {
        return _thumbImage;
    }
    __block UIImage *resultImage;
    
    PHImageRequestOptions *phImageRequestOptions = [[PHImageRequestOptions alloc] init];
    phImageRequestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
    phImageRequestOptions.synchronous = YES;
    
    CGSize imageSize = CGSizeMake(size.width * screenScale, size.height * screenScale);
    [[PHImageManager defaultManager] requestImageForAsset:self.asset
                                               targetSize:imageSize
                                              contentMode:PHImageContentModeAspectFill options:phImageRequestOptions
                                            resultHandler:^(UIImage *result, NSDictionary *info) {
                                                if ([[info valueForKey:@"PHImageResultIsDegradedKey"]integerValue]==0){
                                                    
                                                    resultImage = result;
                                                } else {
                                                    
                                                    resultImage = result;
                                                }
                                            }];
    _thumbImage = resultImage;
    return resultImage;
}


- (void)fetchThumbnailImageWithSize:(CGSize)size completion:(void (^)(UIImage *, NSDictionary *))completion {
    if (_thumbImage) {
        if (completion) {
            completion(_thumbImage, nil);
        }
    } else {
        PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
        imageRequestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;

        [[PHImageManager defaultManager] requestImageForAsset:self.asset targetSize:CGSizeMake(size.width * screenScale, size.height * screenScale) contentMode:PHImageContentModeAspectFill options:imageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
           
            BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue];
            if (downloadFinined) {
                _thumbImage = result;
            }
            if (completion) {
                completion(result, info);
            }
        }];
    }
}

- (UIImage *)previewImage {
    if (_previewImage) {
        return _previewImage;
    }
    __block UIImage *resultImage;
    
    PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
   
    //        imageRequestOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
    imageRequestOptions.version = PHImageRequestOptionsVersionCurrent;
    [[PHImageManager defaultManager]  requestImageForAsset:self.asset
                                                targetSize:CGSizeMake(SCREEN_W, SCREEN_H)
                                               contentMode:PHImageContentModeAspectFill
                                                   options:imageRequestOptions
                                             resultHandler:^(UIImage *result, NSDictionary *info) {
                                                 resultImage = result;
                                             }];
    _previewImage = resultImage;
    return resultImage;
}

- (void)fetchPreviewImageWithCompletion:(void (^)(UIImage *, NSDictionary *))completion withProgressHandler:(PHAssetImageProgressHandler)phProgressHandler {
    if (_previewImage) {
        // 如果已经有缓存的图片则直接拿缓存的图片
        if (completion) {
            completion(_previewImage, nil);
        }
    } else {
        PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
        imageRequestOptions.networkAccessAllowed = YES; // 允许访问网络
        imageRequestOptions.progressHandler = phProgressHandler;
        [[PHImageManager defaultManager] requestImageForAsset:self.asset targetSize:CGSizeMake(SCREEN_W, SCREEN_H) contentMode:PHImageContentModeAspectFill options:imageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
            // 排除取消，错误，低清图三种情况，即已经获取到了高清图时，把这张高清图缓存到 _previewImage 中
            BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue];
            if (downloadFinined) {
                _previewImage = result;
            }
            if (completion) {
                completion(result, info);
            }
        }];
    }
}


#pragma mark - Private Method
+ (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
@end
